// pages/detail/detail.js
const {
  getGoodsDetail,
  getGoodsAppraise,
  getcollect, //收藏接口
  getDetectionCollect, //检测是否收藏接口
  getDelCollect, //删除收藏接口
} = require('../../http/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
      tabs_name: '商品简介',
      view_id: 'swiper-container',
      topHeight: 0
    }, {
      tabs_name: '商品详情',
      view_id: 'goods-des-info',
      topHeight: 0,
    }, {
      tabs_name: '商品评价',
      view_id: 'reputation',
      topHeight: 0,
    }],
    show: false,
    id: '',
    // 轮播图
    slideshow: [],
    // 详情基本数据
    detailTitle: [],
    // 富文本
    text: '',
    // 选择
    select: [],
    // 规格
    specification: [],
    scrollTop: 0,
    sT: 0,
    flag:true,
    active:'goods-des-info'
  },
  showPopup(e) {
    console.log(e);
    // console.log(this.data.detailTitle);
    this.setData({
      show: true,
      specification: e.currentTarget.dataset.item
    })
    console.log(this.data.specification);
  },
  onChange(e) {
    const { name } = e.detail
    console.log(name)
    const query = wx.createSelectorQuery()
    query.select(`#${name}`).boundingClientRect()
    query.exec((res) =>{ 
      const top = res[0].top
      this.data.scrollTop += top - 40
      if(this.data.scrollTop === this.data.sT) {
        this.data.scrollTop = this.data.sT
      }
      this.setData({
        scrollTop: this.data.scrollTop
      })
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  // 收藏
  async collect(e) {

    // let flag = true

    if (this.data.flag) {
      const aa = await getcollect({
        goodsId: this.data.id,
        token: wx.getStorageSync('token')
      })
      console.log(aa);

      // console.log(flag);
      // 检测是否收藏接口
      const bb = await getDetectionCollect({
        goodsId: this.data.id,
        token: wx.getStorageSync('token')
      })
      console.log(bb);
      // flag = false
      this.setData({
        flag:false
      })
      return
    }

    if (this.data.flag === false) {
      // 删除收藏接口

      const cc = await getDelCollect({
        goodsId: this.data.id,
        token: wx.getStorageSync('token')
      })
      // console.log(cc);
      this.setData({
        flag:true
      })
    }




  },
  scrollMove(e) {
    // const { scrollTop } = e.detail
    // this.setData({
    //   sT: scrollTop
    // })
    this.getTopHeightFunction()
    // if(this.data.tabs[0].topHeight <= 0 && this.data.tabs[1].topHeight > 0) {
    //   // console.log(this.data.tabs[0].topHeight);
    //   // console.log(this.data.tabs[1].topHeight);
    //   this.setData({
    //     active: this.data.tabs[0].view_id
    //   })
    //   console.log(this.data.active);
    // } else if(this.data.tabs[1].topHeight <= 0 && this.data.tabs[2].topHeight > 0) {
    //   // console.log();
    //   this.setData({
    //     active: this.data.tabs[1].view_id
    //   })
    //   console.log(this.data.active);
    // } else if(this.data.tabs.length===3) {
    //   this.setData({
    //     active: this.data.tabs[2].view_id
    //   })
    // }
  },
  getTopHeightFunction() {
    var that = this
    var tabs = this.data.tabs
    tabs.forEach((item, index) => {
      var viewId = '#'+item.view_id
      that.getTopHeight(viewId, index)
    })
  },

  getTopHeight(nameId, index) {
    var query = wx.createSelectorQuery()
    query.select(nameId).boundingClientRect()
    query.exec(res => {
      let top = res[0].top
      var tabs = this.data.tabs
      tabs[index].topHeight = top
      this.setData({
        tabs: tabs
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options.id);
    options.id = options.id || '4570'
    this.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    // console.log(this.data.id);
    const res = await getGoodsDetail(this.data.id)
    console.log(res);
    this.setData({
      slideshow: res.data.pics2, //轮播图
      detailTitle: res.data.basicInfo, //详情基本数据
      text: res.data.content, //富文本
      select: res.data.properties //选择
    })
    // console.log(this.data.id);
    const res1 = await getGoodsAppraise(this.data.id)
    // console.log(res1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})